using System;
using System.Collections;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
namespace NCS_CS
{
	internal class CPower
	{
		private _CCard card1;
		//Private TempTime As SYSTEMTIME
		//Private MyTime As SYSTEMTIME

		//Dim ValueArrays(ArrayLen) As Integer
		//Dim MyPoint As int
		//Dim MarginArrays(ArrayLen) As Integer

		//Private mvarWaveData(WaveLen - 1) As Integer
		//Private TGi As int
		//Public BeginedTimes As Integer
		//Public PeakedTimes As Integer
//Private MyVlTm(5000) As ValueTimeType
		//Public MyVlTmCt As int
		//Public StoreCount As int

		//Dim Casevalue As int
		double pps;
		//Dim PrevPowerSet As double
		//Dim Heating As Boolean
		double current;
		double power;
		double volt;
		//Public PrevVolt, PrevCurrent, PrevPower As Integer
		public double deltaPrevPrevPower;
		public double deltaPrevPower;
		public double deltaPower;
		public double PrevOutV;
		public double outv;
		//Public PrevPowerTimeMs As Integer

		//Public Pav As Integer
		//Public pcount As int
		//Dim DeltaPowerO(20) As double
		public double Delta;
		//Private result As Integer
		//Private k As Integer
	

		public double pidp;

		public void setCard(ref _CCard v)
		{
			card1 = v;
		}

		public void StopPID()
		{
            card1.newDA(ModMain.powerChannel, 0);
		}


		public void InitialPID(double powerset)
		{

			deltaPrevPrevPower = 0;
			deltaPrevPower = 0;
			PrevOutV = 0;
			//PrevPowerSet = powerset
			outv = 0;

		}

		public double getTrueVolt()
		{
			return card1.getchanelVolt(0) * 2;
		}
		public double getTrueCurrent()
		{
			return card1.getchanelVolt(1) * 300;
		}

		public double PID(int TimeMs, double powerset, ref double trueVolt, ref double trueCurrent)
		{
			double tmp = 0;
			pps = powerset;
			if (pps > 6000) {
				pps = 6000;
			}
			if ((pps < 1)) {
                card1.newDA(ModMain.powerChannel, 0);
			} else {
				volt = card1.getchanelVolt(0);
				current = card1.getchanelVolt(1);
				trueVolt = getTrueVolt();
				trueCurrent = getTrueCurrent();

				power = trueCurrent * trueVolt;
				if (current > 4.9) {
					if (!static_PID_reportO[0]) {
						modLog.savelogmsg("Current overflow");
						static_PID_reportO[0] = true;
					}
				}
				if (volt > 4.9) {
					if (!static_PID_reportO[1]) {
						static_PID_reportO[1] = true;
						modLog.savelogmsg("Volt overflow");
					}
				}
				Delta = pps - power;
				//Pav = Delta
				if (volt > 1.8 & current < 0.3) {
					if (!static_PID_reportO[2]) {
						modLog.savelogmsg(ModMain.gtran.MyTranslate("1警告:   电流输出为0," +  " 可能发生情况:"  + "1.  未加坩埚或坩埚未放好" +  "2.  采集板上电流检测线未接上", ""));
						static_PID_reportO[2] = true;
					}
				} else if (volt < 0.3 & current > 1.8) {
					if (!static_PID_reportO[3]) {
						modLog.savelogmsg(ModMain.gtran.MyTranslate("2警告:   电压输出为0"  + " 可能发生情况:" + "采集板上电压检测线未接上", ""));
						static_PID_reportO[3] = true;
					}
				} else if (volt < 0.3 & current < 0.3 & outv > 4.9) {
					if (!static_PID_reportO[4]) {
						modLog.savelogmsg(ModMain.gtran.MyTranslate( "4警告:   电流电压输出均为0," + "可能发生情况:"  + "1.  采集板上电压电流检测线均未接上" +  "2.  功率控制输出线未接上" +  "3.  采集板电源未接通", ""));
						static_PID_reportO[4] = true;
					}
				} else {
					deltaPower = Delta / 6000 * 5;
					//            tmp = 2.45 * (deltaPower - deltaPrevPower)
					//            tmp = tmp + 3.5 * deltaPower
					//            tmp = tmp + 1.25 * (deltaPower - 2 * deltaPrevPower + deltaPrevPrevPower)
					tmp = tmp + 0.3F * deltaPower - 0.2F * deltaPrevPower + 0.0625F * deltaPrevPrevPower;
					outv = PrevOutV + pidp * tmp;
					//.13 _.15* tmp '输出电压 为 检测电压变化量的二倍
					//            outv = PrevOutV + tmp    '输出电压
					//            Debug.Print "delta power", deltaPower, deltaPrevPower, outv
					//            outv1 = (outv * 0.00005 + 3.2) '/ 5# * 4096#   '(OutV * (7 - 0.5) / 6000 + 0.5)  '(OutV * (5 - 2.5) / 2048 + 2.5)
					//            outv = PrevOutV + 0.0007 * tmp   '输出电压
					//            outv1 = (outv)   '/ 5# * 4096#   '(OutV * (7 - 0.5) / 6000 + 0.5)  '(OutV * (5 - 2.5) / 2048 + 2.5)
					//            outv1 = outv
                    if (outv >= 4.99)
                    {
                        outv = 4.99F;
                    }
                    //if (outv >= 4F)
                    //{
                    //    outv = 4F;
                    //}
					if (outv < 2.5) {
						outv = 2.5F;
					}
					card1.newDA(ModMain.powerChannel  , outv);
					deltaPrevPrevPower = deltaPrevPower;
					deltaPrevPower = deltaPower;
					PrevOutV = outv;
				}
			}
			return power;
		}
		static bool[] static_PID_reportO = new bool[5];



		public CPower() : base()
		{
			pidp = 0.03F;


		}
	}
}
